/**
 * 后台登录。
 *
 * @author    Fairyin <fairyin@126.com>
 * @copyright © 2016 www.imcn.vip
 * @license   GPL-3.0
 */

$(document).ready(function(){

    if (!$(".scopes_login").length) return;

    if ("object" != typeof _login) _login = {};
    
    //登录
    _login.login = function(){
        var url = common.apiurl + 'shadow/login';
        var params = {
            account: $("input[name='account']").val(),
            password: $("input[name='password']").val()
        };
        common.ajax(url, 'POST', params, function(data){
            _login.redirect(data);
        }, function(error){
            _login.tips(error);
        });
    };
    
    _login.tips = function(data){
        $(".scopes_login .error-tips").parent().removeAttr('style');
        $(".scopes_login .error-tips").html(data.reason);
    };
    
    _login.redirect = function(data)
    {
        if (data.token == false || data.token == undefined) {
            var error = {
                'reason': '跳转失败！~'
            };
            _login.tips(error);
        }
        var token = data.token;
        var date = new Date();
        //一个小时的毫秒数
        var t = date.getTime() + 3600000 * 12;
        var expires = new Date(t);
        var options = {
            'expires': expires,
            'path': '/',
            'domain': '.imcn.vip'
        };
        $.cookie('u', token, options);
        window.location.href = "/shadow/";
    };
    
    _login.hideTips = function(){
        $(".error-tips").parent().attr('style', 'visibility:hidden;');
    };
    
    $("input[name='account']").on('keyup', _login.hideTips);
    $("input[name='password']").on('keyup', _login.hideTips);
    
    $(".scopes_login .btn.btn-success.btn-block").on('click', _login.login);
});